package com.phapli.versionchecking;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.phapli.versionchecking.Adapter.ItemAdapter;
import com.phapli.versionchecking.config.Config;
import com.phapli.versionchecking.object.Item;
import com.phapli.versionchecking.preference.MyPrefs;
import com.phapli.versionchecking.scheduler.AlarmReceiver;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity {

    private ListView listview;
    private MyPrefs myPrefs;
    AlarmReceiver alarm = new AlarmReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Item[] items = Config.initItems();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        int id = -1;
        String version = "";
        if (bundle != null) {
            id = bundle.getInt(Config.intent_name);
            version = bundle.getString(Config.version);
        }
        myPrefs = new MyPrefs(this);

        if (id > 0 && id < items.length) {
            // start from notification bar

            showDialog(items[id], version);


        }else {
            alarm.setAlarm(this);
        }

        listview = (ListView) findViewById(R.id.listview);

        Update update = new Update();
        update.execute(items);



    }

    private void showDialog(final Item item, String version) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        // set title
        alertDialogBuilder.setTitle(getString(R.string.dialog_title) + " " + version + " From " + item.siteName);

        // set dialog message
        alertDialogBuilder
                .setMessage(getString(R.string.dialog_mess))
                .setCancelable(false)
                .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        /*Intent intent_noti = new Intent(Intent.ACTION_VIEW);
                        intent_noti.setData(Uri.parse(item.url));
                        startActivity(intent_noti);
                        */
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(getString(R.string.skip), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        myPrefs.updateBooleanPref(item.id + " notificated", true);
                    }
                })

        ;

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class Update extends AsyncTask<Item, Void, ArrayList<Item>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            /*
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
            */
        }

        @Override
        protected ArrayList<Item> doInBackground(Item... items) {
            // Creating service handler class instance
            ArrayList<Item> result = new ArrayList<>();
            for (int i = 0; i < items.length; i++) {
                if (i == 0) {
                    items[i].version = Config.getVersionName(MainActivity.this, null);
                } else {
                    items[i].version = items[i].parser.parseVersionFromWeb(items[i].url);
                }
                result.add(items[i]);
            }

            return result;
        }

        @Override
        protected void onPostExecute(ArrayList<Item> result) {
            super.onPostExecute(result);

            ItemAdapter adapter = new ItemAdapter(MainActivity.this, result);
            listview.setAdapter(adapter);

        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_continue) {
            startActivity(new Intent(MainActivity.this, InfoActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
